package com.zmz.Stream;

import java.util.*;

/**
 * Create with IntelliT IDEA
 *
 * @Author: zhengmingzhe
 * @Date: 2022/07/31/1:35
 * @Description:  聚合(max,min,count)
 */
public class Stream05 {
    public static  void main(String [] args) {
        //1.获取String集合中长度最长的元素  输出大苏打
        List<String> list =Arrays.asList("李磊", "大苏打","da","saa");
        Optional<String> max=list.stream().max(((o1, o2) -> o1.length()-o2.length()));
        System.out.println(max.get());

        //简化 利用比较器
        Optional<String> max2=list.stream().max((Comparator.comparingInt(String::length)));
        System.out.println(max2.get());

        //2.获取Integer集合中的最大值
        //自然排序
        List<Integer> list2 = Arrays.asList(7, 6, 8, 3, 2, 1, 4);
        System.out.println(list2.stream().max(Integer::compareTo).get());
        System.out.println("_________");
        //自定义排序 找到最小的
        System.out.println(list2.stream().max(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        }).get());
        //简化为Comparator.reverseOrder()
        System.out.println(list2.stream().max(Comparator.reverseOrder()).get());
        System.out.println("_________");

        //计数 统计集合中大于4的个数 结果为3
        System.out.println(list2.stream().filter(t->t>4).count());
        List<Integer> list1 = Arrays.asList(7, 6, 8, 3, 2, 1, 4);
//        筛选大于6的数并遍历
        list1.stream().filter(t -> t > 6).forEach(System.out::println);


        //筛选员工中工资高于8000的人
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("的撒", 8000, 22, "女", "武汉"));
        personList.add(new Person("法按时", 7000, 22, "女", "武汉"));
        personList.add(new Person("的阿萨", 7800, 22, "男", "武汉"));
        personList.add(new Person("答案", 12000, 12, "女", "武汉"));
        personList.add(new Person("滴放", 22000, 21, "女", "北京"));

        //获取员工薪资最高的人
        Person person =personList.stream().max(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getSalary()-o2.getSalary();
            }
        }).get();
        System.out.println(person);
        Person person2 =personList.stream().max(Comparator.comparingInt(Person::getSalary)).get();
        System.out.println(person2);
    }

}
